package cn.cjy.utils;

import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyUtil {
	
    static	Connection conn=null;
	
	public static int update(String sql, Object... args) {
		 PreparedStatement ps=null;
		 int row=0;

		try {
			conn=JDBCConfig.getConnection();
			ps = conn.prepareStatement(sql);
			ParameterMetaData data = ps.getParameterMetaData();// ��ȡԪ���ݶ���
			int count = data.getParameterCount();// ��ȡ�ʺŵĸ���

			for (int i = 0; i < count; i++) {
				ps.setObject(i + 1, args[i]);
			}
			 row=	ps.executeUpdate();
		
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCConfig.closeAll(conn, ps, null);
		}
		return row;

	}
	
	public static <T> T query(String sql, ResultHandle<T> handle, Object... args) {

		conn=JDBCConfig.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try

		{
		
			ps = conn.prepareStatement(sql);
			ParameterMetaData data = ps.getParameterMetaData();
			int count = data.getParameterCount();

			for (int i = 0; i < count; i++) {
				ps.setObject(i + 1, args[i]);

			}

			rs = ps.executeQuery();
			T t = (T) handle.handle(rs);
			return t;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
	
	

}
